﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Entities.Users;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;

namespace CharityGamesOnline.Modules.Common
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
    public class SiteMapProvider : WebControl
    {
        const int SITEMAP_MAXURLS = 50000;

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? "[" + this.ID + "]" : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            output.Write(Text);
        }

        public string BuildSiteMap(int PortalID)
        {
            StringBuilder sb = new StringBuilder();
            TabController objTabs = new TabController();
            foreach (TabInfo objTab in objTabs.GetTabsByPortal(PortalID).AsList())
            {
                if (objTab.IsDeleted == false && objTab.IsVisible && objTab.Level == 0)// && objTab.TabType == TabType.Normal)
                {
                    if (objTab.TabPath == "//Admin" || objTab.TabPath == "//Host")
                        continue;

                    if (PortalSecurity.IsInRoles(objTab.TabPermissions.ToString("VIEW")))
                    {
                        sb.AppendLine("<div class='cgo-footerpage'>");
                        if (objTab.DisableLink)
                        {
                            sb.AppendLine(string.Format("<div class=\"cgo-pgtitles\">{0}</div>", objTab.TabName));
                        }
                        else
                        {
                            sb.AppendLine(string.Format("<div class=\"cgo-pgtitles\"><a href=\"{0}\">{1}</a></div>", objTab.FullUrl, objTab.TabName));
                        }
                        if (objTab.HasChildren)
                        {
                            sb.Append(BuildChildren(PortalID, objTab.TabID)); 
                        }
                        sb.AppendLine("</div>");
                    }
                }
            }
            return sb.ToString();
        }

        public string BuildChildren(int portalID,int parentTabID)
        {
            StringBuilder sb = new StringBuilder();
            foreach (TabInfo objTab in TabController.GetTabsByParent(parentTabID, portalID))
            {
                if (objTab.IsDeleted == false && objTab.IsVisible)// && objTab.TabType == TabType.Normal)
                {
                    if (objTab.DisableLink)
                    {
                        sb.AppendLine(string.Format("<div>{0}</div>", objTab.TabName));
                    }
                    else
                    {
                        sb.AppendLine(string.Format("<div><a href=\"{0}\">{1}</a></div>", objTab.FullUrl, objTab.TabName));
                    }
                }
            }
            return sb.ToString();
        }

        //public string BuildURL(string URL, string PageName, string Description, int Indent, bool hasChild, bool disabled)
        //{
        //    StringBuilder sb = new StringBuilder(1024);

        //    for (int i = 0; i <= Indent; i++)
        //    {
        //        sb.Append("&nbsp;");
        //    }
        //    sb.Append("&#149");
        //    if (disabled)
        //    {
        //        sb.Append(PageName);
        //    }
        //    else
        //    {
        //        sb.Append("<a href='" + URL + "'>" + PageName + "</a>");
        //    }

        //    sb.Append("<br>");
        //    return sb.ToString();
        //}
    }
}
